package com.gitee.tanxianbo.table;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 生成字段SQL
 *
 * @author tanxianbo
 * @create 2018年10月26日
 **/
@Data
public class ColumnSql implements Serializable {

    public ColumnSql(CommonColumn column, List<String> primaryKeys) {
        this.setFieldName(column.getName());
        if (StringUtils.isNoneEmpty(column.getColumnDefinition())) {
            this.setFieldType(column.getColumnDefinition());
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" ");
        stringBuilder.append(column.getType());

        if (column.getLength() > 0) {
            if (column.getDecimalLength() > 0) {
                stringBuilder.append("(").append(column.getLength()).append(",").append(column.getDecimalLength()).append(")");
            } else {
                stringBuilder.append("(").append(column.getLength()).append(")");
            }
        }
        stringBuilder.append(" ");
        if (column.isNullValue() && !primaryKeys.contains(column.getName())) {
            stringBuilder.append("NULL");
        } else {
            stringBuilder.append("NOT NULL");
        }
        if (column.isAutoIncrement()) {
            stringBuilder.append(" ");
            stringBuilder.append("AUTO_INCREMENT");
        }
        if (StringUtils.isNoneEmpty(column.getDefaultValue())) {
            stringBuilder.append(" ");
            stringBuilder.append("DEFAULT " + column.getDefaultValue());
        }
        this.setFieldType(stringBuilder.toString());
    }

    /**
     * 字段类型
     */
    private String fieldType;

    /**
     * 字段名称
     */
    private String fieldName;
}
